# import sys
#
# print(sys.argv[0])

# f = open(file="text.txt", mode="w")
# f.write("hello world\n")
# f.close()

# f = open(file="text.txt", mode="a")
# f.write("hello Python,我爱你")
# f.close()

# f = open(file="text.txt")
# for line in f:
#     # print(line,end="")
#     line = line.split()
#     height = int(line[2])
#     weight = int(line[3])
#     if height >= 170 and weight <= 50:
#         print(line)


# 打开图片
# f = open("year.jpg", "r")
# for line in f:
#     print(line)

# f = open("year.jpg", "rb")
# for line in f:
#     print(line)

# f = open(file="photo",mode="wb")
# s = "我爱你\n"
# f.write(s.encode("utf-8"))
#
# f = open("photo",mode="ab")
# S = "但是还是算了"
# f.write(S.encode("utf-8"))

# f = open("year.jpg",mode="rb")
# for line in f:
#     print(line)

# f = open("text.txt", mode="a")
# # f.write("hello world")
# f.write("\n")
# f.write("ok?\n")
# f.write("woshi")
# f.flush()
# f = open("text.txt", "a")
# # print(f.tell())
# # f.write("hello world\n")
# print(f.tell())
# f.seek(450)
# #f.write("------")
# print(f.tell())

# f = open("text_file.txt", mode="w")
# f.write("hello Python!")
# print(f.tell())
# f.seek(0)
# f.write("-----")

# f = open("text_file.txt", "w")
# f.write("hello Python!\n")
# f.write("zhangsan\n")
# f.flush()
# f.write("lisi\n")
# f.write("wangwu\n")
# f.write("zhaoliu\n")
# print(f.tell())
# # f.tell()告诉当前光标所在位置
# f.seek(14)
# print(f.tell())
# f.close()
#
# f = open("text.txt",mode="r+",encoding="utf-8")
# print(f.tell())
# f.write("这是新插入的字符串")
# f.seek(40)
# f.write("我是真相")
# # f.seek(10)
# # f.write("在这插入一段文字")
# print(f.tell())


# char = "zhangsan lisi zhangsan lisi zhangsan lisi zhangsan lisi"
# print(char.replace("zhangsan","wangwu",2))

#
# 文本的创建和打开
# 创建
# f = open(file="text_file_1",mode="w", encoding="utf-8")
# f.write("hello Python!")
# f.close()
# 打开
# f = open("text_file_1",mode="w",encoding="utf-8")
# f.write("My name is zhangsan and I love Python!")
# f.close()

# f = open("text_file_1",mode="a",encoding="utf-8")
# f.write("\n")
# f.write("I am 20 years old\n")
# f.close()

# mode有三种：“r” "w" "a"

# f = open("text_file_1")
# print(f.readline(),end="")
# print(f.readline(),end="")
# print(f.readline(),end="")
# # 想要把最后的回车去掉则 end = “”

# f = open("text_file_1",mode="r",encoding="utf-8")
# ret = f.read()
# print(ret)

# f = open("text_file_1", mode="r", encoding="utf-8")
# for i in f:
#     ret = i.split()
#     height = int(ret[2])
#     weight = int(ret[3])
#     if height >= 170 and weight <= 50:
#         print(ret)

# s = "张三"
# ret = s.encode("utf-8")
# ret2 = ret.decode("utf-8")
# print(ret)
# print(ret2)


# s = "李四"
# ret = s.encode("gbk")
# ret2 = ret.decode("gbk")
# print(ret)
# print(ret2)

# 编码不同
# s = "李四"
# ret = s.encode("gbk")
# ret2 = ret.decode()
# print(ret)
# print(ret2)

# f = open("year.jpg", mode="rb")
# for line in f:
#     print(line)

# seek tell flush
# f = open("text_file.txt","w",encoding="utf-8")
# f.write("hello Python !")
# print(f.tell())
# f.write("\n")
# f.write("My name is zhangsan")
# f.seek(0)
# f.write("what!")
# print(f.tell())
# f.write("wowowo")
# f.flush()
# f.close()
# seek跳转
# tell光标位置
# flush缓存

# 混合模式
#  --- r+ w+ a+
# f = open("text_file.txt",mode="r+",encoding="utf-8")
# ret = f.read()
# print(ret)
# f.write("hello world\n")

# f = open("text_file.txt",mode="a",encoding="utf-8")
# f.seek(0)
# f.write("我在学习Python！")
# f.seek(20)
# f.write("这是一段字符串")

# f = open("text_file.txt",mode="r+")
# f.seek(0)
# f.truncate()

# 修改
# f = open("text_file.txt", mode="r+", encoding="utf-8")
# ret = f.read()
# f.seek(0)
# f.truncate()
# ret2 = ret.replace("北京", "浙江")
# f.write(ret2)
# f.close()


# f = open("text_file.txt",mode="r+",encoding="utf-8")
# result = f.read()
# f.seek(0)
# f.truncate()
# ret = result.replace("深圳","广东")
# f.write(ret)
# f.close()
# 替换
# （1） 存储
# （2） 删除
# （3） 替换

import sys

print(sys.argv)
old_str = sys.argv[1]
new_str = sys.argv[2]
filenam = sys.argv[3]

f = open(filename, mode="r+")
ret = f.read()

old_str_count = f.count(old_str)
f.seek(0)
f.truncate()

ret2 = ret.replace(old_str, new_str)
f.write(ret2)
print(f"一共修改{old_str_count}处，将{old_str}改为{new_str}", "修改完毕")
